package org.endy.leisure.diaTracker.controller

import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import kotlinx.coroutines.reactor.mono
import org.endy.leisure.diaTracker.entity.User
import org.endy.leisure.diaTracker.entity.announcement.Announcement
import org.endy.leisure.diaTracker.entity.announcement.ReadAnnouncement
import org.endy.leisure.diaTracker.service.AnnouncementService
import org.endy.leisure.diaTracker.util.CommUtils
import org.springframework.beans.factory.annotation.Value
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import reactor.core.publisher.Mono

@Tag(name = "公告接口", description = "用于医生发布公告和阅读公告")
@RestController
@RequestMapping("/ann")
class AnnouncementController(private val announcementService: AnnouncementService) {

    @Value("\${web.pic.dir:/data/nginx/www/images}")
    private lateinit var imgSavePath: String

    @Operation(summary = "公告发布接口", description = "用于医生发布公告")
    @PostMapping("/publish")
    fun publish(@RequestBody request: AnnouncementRequest): Mono<WebResponse<String>> = mono {

        val imagePath = request.image?.let { imageBase64Str ->
            CommUtils.saveBase64ImageToFile(imageBase64Str, imgSavePath)
        }

        val announcement = Announcement(
            title = request.title,
            content = request.content,
            type = request.type,
            publisher = request.publisher,
            imagePath = imagePath,
            expireTime = request.expireTime,
            user = User(request.userId)
        )
        announcementService.publisher(announcement)
        WebResponse.success("公告【${request.title}】发布成功")
    }

    @Operation(summary = "公告带状态查询接口", description = "用于用户查询公告")
    @PostMapping("/listWithStatus")
    fun announcementList(@RequestBody request: UserIdRequest): Mono<WebResponse<List<ReadAnnouncement>>> = mono {
        val list = announcementService.queryAnnouncementStatus(request.userId)
        WebResponse.success(list)
    }

    @Operation(summary = "公告查看接口", description = "用于用户查看公告详情")
    @PostMapping("/annView")
    fun announcementView(@RequestBody request: AnnouncementViewRequest): Mono<WebResponse<Announcement>> = mono {
        val ann = announcementService.readAnnouncement(request.userId,request.announcementId)
        WebResponse.success(ann)
    }


    @Operation(summary = "公告带列表查询接口", description = "用于发布者查询公告")
    @PostMapping("/all")
    fun all(): Mono<WebResponse<List<Announcement>>> = mono {
        val list = announcementService.queryAll()
        WebResponse.success(list)
    }

}